//
//  DropListVC.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/17.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit

public class DropListCell: UITableViewCell {
    @IBOutlet weak var wrapper: UIView!
    @IBOutlet weak var coverIV: UIImageView!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var authorAvatarIV: UIImageView!
    @IBOutlet weak var authorNameLabel: UILabel!
	@IBOutlet weak var dateLabel: UILabel!
    
    var drop: Drop!{
        didSet{
            contentLabel.attributedText = drop.content.attributedText
            coverIV.image = UIImage.named(drop.image)
            
            authorNameLabel?.text = drop.author.name
			dateLabel?.text = drop.date.shortText
        }
    }
}

class DropListVC: UITableViewController {

    var drops: [Drop]!
    
    var dataObject = ""{
        didSet{
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        drops = Drop.sampleData()
        drops.forEach{ item in
            item.content.alignment = 1
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let cell = sender as? UITableViewCell,
            let indexPath = tableView.indexPath(for: cell),
            let vc = segue.destination as? DropDetailVC{
            vc.drop = drops[indexPath.row]
        }
    }
    
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return drops.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueCell(withClass: DropListCell.self, for: indexPath)!
        
        cell.drop = drops[indexPath.row]
        
        return cell
    }

}
